package com.zzj.filedownloader.download.dbcontrol;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.zzj.filedownloader.download.dbcontrol.bean.SQLDownLoadInfo;
import java.util.ArrayList;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

/* loaded from: classes2.dex */
public class DataKeeper {
    private SQLiteDatabase db;
    private SQLiteHelper dbhelper;
    private int doSaveTimes = 0;

    public DataKeeper(Context context) {
        this.dbhelper = new SQLiteHelper(context);
    }

    public void saveDownLoadInfo(SQLDownLoadInfo sQLDownLoadInfo) {
        Cursor cursor;
        ContentValues contentValues = new ContentValues();
        contentValues.put("taskID", sQLDownLoadInfo.getTaskID());
        contentValues.put("downLoadSize", Long.valueOf(sQLDownLoadInfo.getDownloadSize()));
        contentValues.put("fileName", sQLDownLoadInfo.getFileName());
        contentValues.put("filePath", sQLDownLoadInfo.getFilePath());
        contentValues.put("fileSize", Long.valueOf(sQLDownLoadInfo.getFileSize()));
        contentValues.put(IjkMediaPlayer.OnNativeInvokeListener.ARG_URL, sQLDownLoadInfo.getUrl());
        try {
            this.db = this.dbhelper.getWritableDatabase();
            cursor = this.db.rawQuery("SELECT * from downloadinfo WHERE taskID = ? ", new String[]{sQLDownLoadInfo.getTaskID()});
            try {
                if (cursor.moveToNext()) {
                    this.db.update(SQLiteHelper.TABLE_NAME, contentValues, "taskID = ? ", new String[]{sQLDownLoadInfo.getTaskID()});
                } else {
                    this.db.insert(SQLiteHelper.TABLE_NAME, null, contentValues);
                }
                cursor.close();
                this.db.close();
            } catch (Exception unused) {
                this.doSaveTimes++;
                if (this.doSaveTimes < 5) {
                    saveDownLoadInfo(sQLDownLoadInfo);
                } else {
                    this.doSaveTimes = 0;
                }
                if (cursor != null) {
                    cursor.close();
                }
                SQLiteDatabase sQLiteDatabase = this.db;
                if (sQLiteDatabase != null) {
                    sQLiteDatabase.close();
                }
                this.doSaveTimes = 0;
            }
        } catch (Exception unused2) {
            cursor = null;
        }
        this.doSaveTimes = 0;
    }

    public SQLDownLoadInfo getDownLoadInfo(String str) {
        SQLDownLoadInfo sQLDownLoadInfo;
        this.db = this.dbhelper.getWritableDatabase();
        Cursor rawQuery = this.db.rawQuery("SELECT * from downloadinfoWHERE taskID = ? ", new String[]{str});
        if (rawQuery.moveToNext()) {
            sQLDownLoadInfo = new SQLDownLoadInfo();
            sQLDownLoadInfo.setDownloadSize(rawQuery.getLong(rawQuery.getColumnIndex("downLoadSize")));
            sQLDownLoadInfo.setFileName(rawQuery.getString(rawQuery.getColumnIndex("fileName")));
            sQLDownLoadInfo.setFilePath(rawQuery.getString(rawQuery.getColumnIndex("filePath")));
            sQLDownLoadInfo.setFileSize(rawQuery.getLong(rawQuery.getColumnIndex("fileSize")));
            sQLDownLoadInfo.setUrl(rawQuery.getString(rawQuery.getColumnIndex(IjkMediaPlayer.OnNativeInvokeListener.ARG_URL)));
            sQLDownLoadInfo.setTaskID(rawQuery.getString(rawQuery.getColumnIndex("taskID")));
        } else {
            sQLDownLoadInfo = null;
        }
        rawQuery.close();
        this.db.close();
        return sQLDownLoadInfo;
    }

    public ArrayList<SQLDownLoadInfo> getAllDownLoadInfo() {
        ArrayList<SQLDownLoadInfo> arrayList = new ArrayList<>();
        this.db = this.dbhelper.getWritableDatabase();
        Cursor rawQuery = this.db.rawQuery("SELECT * from downloadinfo", null);
        while (rawQuery.moveToNext()) {
            SQLDownLoadInfo sQLDownLoadInfo = new SQLDownLoadInfo();
            sQLDownLoadInfo.setDownloadSize(rawQuery.getLong(rawQuery.getColumnIndex("downLoadSize")));
            sQLDownLoadInfo.setFileName(rawQuery.getString(rawQuery.getColumnIndex("fileName")));
            sQLDownLoadInfo.setFilePath(rawQuery.getString(rawQuery.getColumnIndex("filePath")));
            sQLDownLoadInfo.setFileSize(rawQuery.getLong(rawQuery.getColumnIndex("fileSize")));
            sQLDownLoadInfo.setUrl(rawQuery.getString(rawQuery.getColumnIndex(IjkMediaPlayer.OnNativeInvokeListener.ARG_URL)));
            sQLDownLoadInfo.setTaskID(rawQuery.getString(rawQuery.getColumnIndex("taskID")));
            arrayList.add(sQLDownLoadInfo);
        }
        rawQuery.close();
        this.db.close();
        return arrayList;
    }

    public void deleteDownLoadInfo(String str) {
        this.db = this.dbhelper.getWritableDatabase();
        this.db.delete(SQLiteHelper.TABLE_NAME, "taskID = ? ", new String[]{str});
        this.db.close();
    }

    public void deleteAllDownLoadInfo() {
        this.db = this.dbhelper.getWritableDatabase();
        this.db.delete(SQLiteHelper.TABLE_NAME, null, null);
        this.db.close();
    }
}
